package com.tedu.myblog.controller.admin;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tedu.myblog.entity.Type;
import com.tedu.myblog.service.TypeService;


@Controller
@RequestMapping("/admin")
public class TypeController {

	@Autowired
	private TypeService typeService;

	// 分页查询分类列表
	@GetMapping("/types")
	public String list(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
		//按照排序字段 倒序 排序
		String orderBy = "id desc";
		PageHelper.startPage(pageNum,10,orderBy);
		List<Type> list = typeService.getAllType();
		PageInfo<Type> pageInfo = new PageInfo<Type>(list);
		model.addAttribute("pageInfo",pageInfo);
		return "admin/types";
	}

	//    返回新增分类页面
	@GetMapping("/types/input")
	public String input(Model model){
		model.addAttribute("type", new Type());
		return "admin/types-input";
	}

	//  新增分类
	@PostMapping("/types")
	public String post(@Valid Type type, RedirectAttributes attributes) {
		Type type1 = typeService.getTypeByName(type.getName());
		if (type1 != null) {
			attributes.addFlashAttribute("message", "不能添加重复的分类");
			return "redirect:/admin/types/input";
		}
		int t = typeService.saveType(type);
		if (t == 0) {
			attributes.addFlashAttribute("message", "新增失败");
		} else {
			attributes.addFlashAttribute("message", "新增成功");
		}
		return "redirect:/admin/types";
	}

	//    跳转修改分类页面
	@GetMapping("/types/{id}/input")
	public String editInput(@PathVariable Long id, Model model) {
		model.addAttribute("type", typeService.getType(id));
		return "admin/types-input";
	}

	//    编辑修改分类
	@PostMapping("/types/{id}")
	public String editPost(@Valid Type type, RedirectAttributes attributes) {
		Type type1 = typeService.getTypeByName(type.getName());
		if (type1 != null) {
			attributes.addFlashAttribute("message", "不能添加重复的分类");
			return "redirect:/admin/types/input";
		}
		int t = typeService.updateType(type);
		if (t == 0 ) {
			attributes.addFlashAttribute("message", "编辑失败");
		} else {
			attributes.addFlashAttribute("message", "编辑成功");
		}
		return "redirect:/admin/types";
	}

	//    删除分类
	@GetMapping("/types/{id}/delete")
	public String delete(@PathVariable Long id,RedirectAttributes attributes) {
		typeService.deleteType(id);
		attributes.addFlashAttribute("message", "删除成功");
		return "redirect:/admin/types";
	}

}

